package com.itheima.controller.backend.security.admin;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.pojo.vo.MemberVO;
import com.itheima.common.pojo.vo.OrderReportVO;
import com.itheima.common.pojo.vo.SetmealVO;
import com.itheima.common.pojo.vo.BusinessReportVO;
import com.itheima.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;

    /**
     * 会员统计
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        log.info("开始查询会员统计数据");
        // 设置默认时间范围：结束时间为当前月，开始时间为2个月前
        LocalDate end = LocalDate.now();
        LocalDate begin = end.minusMonths(1); // 获取1个月前的日期，这样加上当前月正好是2个月

        // 将日期调整到月初和月末
        begin = begin.withDayOfMonth(1); // 设置为月初第一天
        end = end.withDayOfMonth(end.lengthOfMonth()); // 设置为月末最后一天

        MemberVO memberVO = reportService.getMemberReport(begin,end);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,memberVO);
    }

    /**
     * 获取套餐占比统计数据
     * @return
     */
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        log.info("开始查询套餐占比统计数据");
        SetmealVO setmealVO = reportService.getSetmealReport();
        return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,setmealVO);
    }

    /**
     * 预约到诊数据统计
     */
    @GetMapping("/getOrderReport")
    public Result getOrderReport() {
        log.info("开始查询预约到诊数据统计");
        OrderReportVO orderReport = reportService.getOrderReport();
        return new Result(true, MessageConstant.GET_ORDER_REPORT_SUCCESS,orderReport);
    }


    /**
     * 获取运营统计数据
     * @return
     */
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        log.info("开始获取运营统计数据");
        BusinessReportVO businessReportVO = reportService.getBusinessReportData();
        return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, businessReportVO);
    }

    /**
     * 导出运营数据报表
     * @param response
     */
    @GetMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletResponse response){
        log.info("开始导出运营数据报表");
        reportService.exportBusinessReport(response);
    }

}
